/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.cud.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.iwsh.core.utils.IDGenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.us.usws.biz.ResourceStorageReplacer;
import com.us.usws.dao.ChannelDAO;
import com.us.usws.model.domain.channel.Channel;
import com.us.usws.model.flat.channel.DOChannel;
import com.us.usws.service.cud.ChannelCUDService;

/**
 * Service RO impl class
 * @author robot
 */
public class ChannelCUDServiceImpl implements ChannelCUDService {

	/**
	 * DAO instance
	 */
	private ChannelDAO dao = new ChannelDAO();

	/**
	 * Insert one row.
	 */
	@Override
	public String addChannel(DOChannel channel) {
		String uid = IDGenerator.getInstance().genUID();
		channel.setUid(uid);

		String picture = channel.getPicture();
		if (StringUtils.isNotBlank(picture)) {
			int index = picture.lastIndexOf("/");
			if (index != -1) {
				channel.setPicture(picture.substring(index + 1));
				try {
					FileUtils.copyFile(new File(picture),
							new File(ResourceStorageReplacer.getChannelPicPath(channel.getUid(), channel.getPicture())));
					dao.insertChannel(channel);
					return uid;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * Update by PK.
	 */
	@Override
	public void updateChannel(DOChannel channel) {
		String picture = channel.getPicture();
		if (StringUtils.isNotBlank(picture) && picture.contains("/")) {
			int index = picture.lastIndexOf("/");
			if (index != -1) {
				channel.setPicture(picture.substring(index + 1));
				try {
					FileUtils.copyFile(new File(picture),
							new File(ResourceStorageReplacer.getChannelPicPath(channel.getUid(), channel.getPicture())));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		dao.updateChannel(channel);
	}

	/**
	 * Update is valid flag by PK.
	 */
	@Override
	public void updateIsValid(String isValid, String uid) {
		dao.updateIsValid(isValid, uid);
	}

	/**
	 * Delete row by PK.
	 */
	@Override
	public void deleteChannel(String uid) {
		dao.deleteChannel(uid);
	}

	@Override
	public void deleteChannelMore(String uids) {
		if (StringUtils.isNotBlank(uids)) {
			String[] uid = StringUtils.split(uids, ",");
			for (String id : uid) {
				deleteChannel(id);
			}
		}
	}

	@Override
	public boolean exchangeChannelPosition(String uids) {
		if (StringUtils.isNotBlank(uids)) {
			String[] uid = StringUtils.split(uids, ",");
			if (uid.length == 2) {
				Channel firstChannel = dao.getChannel(uid[0]);
				Channel secondChannel = dao.getChannel(uid[1]);

				/*
				 * exchange seqNum
				 */
				int seqNum = firstChannel.getSeqNum();
				firstChannel.setSeqNum(secondChannel.getSeqNum());
				secondChannel.setSeqNum(seqNum);
				List<DOChannel> channels = new ArrayList<DOChannel>();
				channels.add(firstChannel);
				channels.add(secondChannel);
				dao.updateChannels(channels);
				return true;
			}
		}
		return false;
	}
	
	public void addChannelVideo(String[] videoUids, String channelUid, String createByUid) {
		if(videoUids != null && StringUtils.isNotBlank(channelUid) && StringUtils.isNotBlank(createByUid)) {
			dao.addChannelVideo(videoUids, channelUid, createByUid);
		}
	}
	
	public void deleteChannelVideo(String[] videoUids, String channelUid) {
		if(videoUids != null && StringUtils.isNotBlank(channelUid)) {
			dao.deleteChannelVideo(videoUids, channelUid);
		}
	}
	
	public void topChannelVideo(String[] videoUids, String channelUid, int[] seqNums) {
    	if(videoUids != null && StringUtils.isNotBlank(channelUid) && seqNums != null) {
			dao.topChannelVideo(videoUids, channelUid, seqNums);
		}
    }
	
	public void recommentChannelVideo(String[] videoUids, String channelUid, String hot){
		if(videoUids != null && StringUtils.isNotBlank(channelUid)) {
			dao.recommentChannelVideo(videoUids, channelUid, hot);
		}
	}

}